package com.xxx.datalake.utils.json;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Set;

/**
 * @ Author: Aneve
 * @ Project: structured-data
 * @ Since: 2022/9/14 19:23
 * @ Description:多隆镇楼，bug退散🙏🙏🙏
 */
public class CreateJson {


    /**
     * 编写MySQL->hbase的json文件
     * @param writerTable           写入hbase中的表
     * @throws JSONException
     * @throws SQLException
     */
    public String Mysql2HbaseJson(HashMap<String,String> fieldMap, String writerTable, String columnFamilyName,String rowKey,String fileName,HttpSession session) throws JSONException{
        CreateJsonFileUtils jsonFileUtils = new CreateJsonFileUtils();
        JSONObject jsonObject = new JSONObject();

        JSONObject job = new JSONObject();
        JSONObject speed = new JSONObject();
        speed.put("channel",3);
        JSONObject setting = new JSONObject();
        setting.put("speed",speed);
        job.put("setting",setting);

        //reader
        JSONObject reader = new JSONObject();
        reader.put("name","mysqlreader");

        JSONObject paramReader = new JSONObject();
        JSONArray columnReader = new JSONArray();

        for (String column: fieldMap.keySet()){
            columnReader.put(column);
        }

        JSONArray table = new JSONArray();
        JSONArray jdbcUrl = new JSONArray();

        table.put(session.getAttribute("mysqlTableName"));
/*        String s = "jdbc:mysql://localhost:3306/" + readerDB + "?serverTimezone=Asia/" + "Shanghai&useLegacyDatetimeCode=false&useSSL=false&nullNamePatternMatchesAll=true&useUnicode=true&characterEncoding=UTF-8";*/

        jdbcUrl.put(session.getAttribute("jdbcUrl"));
        JSONObject conn1 = new JSONObject();
        conn1.put("table",table);
        conn1.put("jdbcUrl",jdbcUrl);

        JSONArray conn = new JSONArray();
        conn.put(conn1);

        paramReader.put("username",session.getAttribute("jdbcUsername"));
        paramReader.put("password",session.getAttribute("jdbcPassword"));
        paramReader.put("column",columnReader);
        paramReader.put("connection",conn);

        reader.put("parameter",paramReader);



        //write
        JSONObject rowKeyCol1 = new JSONObject();
        int j = 0;
        for (String column: fieldMap.keySet()) {
            if (rowKey.equals(column)){
                rowKeyCol1.put("index",j);
            }else {
                j++;
            }
        }
        rowKeyCol1.put("type","string");
        JSONArray rowKeyCol = new JSONArray();
        rowKeyCol.put(rowKeyCol1);

        JSONArray colWriter = new JSONArray();
        int i = 0;
        for (String column: fieldMap.keySet()) {
            JSONObject col = new JSONObject();
            col.put("index",i++);
            col.put("name",columnFamilyName + ":" + fieldMap.get(column));
            col.put("type","string");
            colWriter.put(col);
        }


        /*String zookeeper = "192.168.239.124:2181,192.168.239.125:2181,192.168.239.126:2181";*/
        JSONObject hbaseConfig = new JSONObject();
        hbaseConfig.put("hbase.zookeeper.quorum",session.getAttribute("hbaseUrl"));

        JSONObject paramWriter = new JSONObject();
        paramWriter.put("hbaseConfig",hbaseConfig);
        paramWriter.put("table",writerTable);
        paramWriter.put("mode","normal");
        paramWriter.put("rowkeyColumn",rowKeyCol);
        paramWriter.put("column",colWriter);
        paramWriter.put("encoding","utf-8");

        JSONObject writer = new JSONObject();
        writer.put("name","hbase11xwriter");
        writer.put("parameter",paramWriter);


        JSONObject content1 = new JSONObject();
        content1.put("reader",reader);
        content1.put("writer",writer);
        JSONArray content = new JSONArray();
        content.put(content1);


        job.put("content",content);

        jsonObject.put("job",job);


        String filePath = "D:\\Aneve-1202121489\\testJson";
        String jsonString = jsonObject.toString();
        String jsonFile = jsonFileUtils.createJsonFile(jsonString, filePath, fileName);

        return jsonFile;
    }
}
